package controller;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.opensymphony.xwork2.ModelDriven;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
//import_header//
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class //Controller//Controller extends AppController implements ModelDriven<//Header//> {

    
    //Header// //header// = new //Header//();
    ArrayList<//Header//> //headers// = new ArrayList();    
    //Header//DAO //header//Dao = new //Header//Model();

    private InputStream inputStream;
    Map parameters;

    public InputStream getInputStream() {
        return inputStream;
    }


//param_header//

    @Override
    public //Header// getModel() {
        return //header//;
    }

    public String index() {
        total = //header//Dao.count();
        page_size=10;
        //headers// = //header//Dao.getList(page_size, page_number);
        return "index";
    }

    public String add() {       
//param_header_all//
        return "add";       
    }

    public String create() {
    
      SessionFactory factory;

    
        try {
            factory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Failed to create sessionFactory object." + ex);
            throw new ExceptionInInitializerError(ex);
        }
   
        Session session = factory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();

            parameters = ActionContext.getContext().getParameters();

//py_save_header//
            session.save(//header//);

            for (int i = 0; i < Integer.parseInt(getParameterValue("items")); i++) {
                //Detail// //detail// = new //Detail//();
                //py_save_detail//
                //detail//.set//Header//(//header//);
                session.save(//detail//);
            }


            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            System.err.println(e.getMessages());            
        } finally {
            session.close();
        }    
        String s = "hola";
        try {
            inputStream = new ByteArrayInputStream(s.getBytes("UTF8"));
        } catch (UnsupportedEncodingException ex) {
             System.err.println(ex.getMessage());     
        }
        return "success";
    }

    public String read() {
        //header// = //header//Dao.read(//header//.getId());
        return "read";
    }

    public String getParameterValue(String param){  
        Object paramObj = parameters.get(param);  
        if(paramObj == null) return null;  
        return ((String[])paramObj)[0];  
    }

//param_header_get_set//

//views_str//


}
